﻿using System;

namespace ParserGenerator.Extensions
{
    public static class ConsoleExtensions
    {
        public const ConsoleColor PromptColor = ConsoleColor.Cyan;
        public const ConsoleColor InputColor = ConsoleColor.Green;

        public const ConsoleColor ErrorColor = ConsoleColor.Red;
        public const ConsoleColor WarningColor = ConsoleColor.Cyan;
        public const ConsoleColor MessageColor = ConsoleColor.Yellow;
        public const ConsoleColor HighlightColor = ConsoleColor.White;
        public const ConsoleColor DebugColor = ConsoleColor.DarkGray;

        public const ConsoleColor AddedColor = ConsoleColor.Green;
        public const ConsoleColor RemovedColor = ConsoleColor.Red;

        public static bool StdInIsInteractive => !Console.IsInputRedirected;
        public static bool StdOutIsInteractive => !Console.IsOutputRedirected;
        public static bool StdErrorIsInteractive => !Console.IsErrorRedirected;
        public static bool ConsoleIsInteractive => StdInIsInteractive && StdOutIsInteractive && StdErrorIsInteractive;

        public static IDisposable AsForegroundColor(this ConsoleColor color) => new WithColor(color);

        [Serializable]
        private sealed class WithColor : IDisposable
        {
            private readonly ConsoleColor _saved;

            public WithColor(ConsoleColor color)
            {
                _saved = Console.ForegroundColor;
                Console.ForegroundColor = color;
            }

            public void Dispose()
            {
                Console.ForegroundColor = _saved;
            }
        }
    }
}
